window.onload = function(){
  $("#toPhone").click(function(){
    $(".mz-login-1").css({ "display": "none" });
    $(".mz-login-2").css({ "display": "block" });
  });
  $('#toPc').click(function(){
    $(".mz-login-1").css({ "display": "block" });
    $(".mz-login-2").css({ "display": "none" });
  });

  $(".mz-login-1 button").click(login1);
  $(".mz-login-2 button").click(login2);

  $(".mz-close").click(function(){
    tips()
  })
}


// 登录事件函数
function login1(){
  let $uname = uname.value;
  let $upwd = upwd.value;
  if(!$uname){
    tips("请填写完整的登录信息",true)
  }else if(!$upwd){
    tips("请填写完整的登录信息",true)
  }else{
    tips()
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function(){
      if(xhr.readyState === 4 && xhr.status === 200){
        let rs = xhr.responseText;
        if(rs==0){
          tips("账号不存在",true)
        }else if(rs==1){
          location.href = "index.html?login";
        }
      }
    }
    let url = `http://127.0.0.1:8080/mz/login1`;
    console.log(url);
    xhr.open("post",url,true);
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xhr.send(`uname=${$uname}&upwd=${$upwd}&phone=${$uname}`);
  }
}

// 手机号码登录事件函数
function login2(){
  let $phone = uphone.value;
  if(!$phone){
    tips("请填写完整的登录信息",true)
  }else{
    tips()
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function(){
      if(xhr.readyState === 4 && xhr.status === 200){
        let rs = xhr.responseText;
        if(rs==0){
          tips("账号不存在",true)
        }else if(rs==1){
          location.href = "index.html?login";
        }
      }
    }
    let url = `http://127.0.0.1:8080/mz/login2`;
    console.log(url);
    xhr.open("post",url,true);
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xhr.send(`phone=${$phone}`);
  }
}

// tips框显示隐藏
// bool为是否隐藏，true为显示
function tips(text,bool){
  if(bool){
    $(".mz-tips").css("visibility","visible")
    $(".mz-tips-info").html(`${text}`)
  }else{
    $(".mz-tips").css("visibility","hidden")
  }
}